<?php

App::uses('AppModel', 'Model');

/**
 * Product Model
 *
 * @property Suppliers $Suppliers
 */
class Product extends AppModel {

    /**
     * Validation rules
     *
     * @var array
     */
    public $validate = array(
        'productscategories_id' => array(
            //'notempty' => array(
                //'rule' => array('notempty'),
                //'message' => 'Please choose a category',
                //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            //),
        ),
        'productsbrands_id' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                'message' => 'Please enter a brand name',
                'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'productsranges_id' => array(
            //'notempty' => array(
                //'rule' => array('notempty'),
                //'message' => 'Please enter a product range',
                //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
           // ),
        ),
        'description' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                'message' => 'Please enter a description',
                //'message' => 'Your custom message here',
                'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'size' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                'message' => 'Please enter a size (125ml)',
                //'message' => 'Your custom message here',
                'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'cost_price' => array(
            'cp-1' => array(
                'rule' => array('numeric'),
                'message' => 'Number only please',
                //'allowEmpty' => false,
                //'required' => false,
                'last' => true, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
            'cp-2' => array(
                'rule' => array('range', 0, 1000),
                'message' => 'Please enter a number between 0 and 1000',
            ),
        ),
        'retail_price' => array(
            'rrp-1' => array(
                'rule' => array('numeric'),
                'message' => 'Number only please',
                //'allowEmpty' => false,
                //'required' => false,
                'last' => true, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
            'rrp-2' => array(
                'rule' => array('range', 0, 1000),
                'message' => 'Please enter a number between 0 and 1000',
            ),
        ),
        'min_level' => array(
            'min-1' => array(
                'rule' => array('numeric'),
                'message' => 'Number only please',
                //'allowEmpty' => false,
                //'required' => false,
                'last' => true, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
            'min-2' => array(
                'rule' => array('range', 0, 1000),
                'message' => 'Please enter a number between 0 and 1000',
            ),
            'below' => array(
                'rule' => array('belowMax'),
                'message' => 'Minimum level has to be less or equal maximum level'
            )
        ),
        'max_level' => array(
            'max-1' => array(
                'rule' => array('numeric'),
                'message' => 'Number only please',
                //'allowEmpty' => false,
                //'required' => false,
                'last' => true, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
            'max-2' => array(
                    'rule' => array('range', 0, 1000),
                    'message' => 'Please enter a number between 0 and 1000',
                
            ),
        ),
        'onhand_level' => array(
            'onhand-1' => array(
                'rule' => array('numeric'),
                'message' => 'Number only please',
                //'allowEmpty' => false,
                //'required' => false,
                'last' => true, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
            'onhand-2' => array(
                    'rule' => array('range', 0, 1000),
                    'message' => 'Please enter a number between 0 and 1000',
                
            ),
        ),
        'suppliers_id' => array(
            /*'numeric' => array(
                'rule' => array('numeric'),
                //'message' => 'Your custom message here',
                'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),*/
        ),
    );

    //The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * belongsTo associations
 *
 * @var array
 */
    public $belongsTo = array(
        'Productsbrand' => array(
            'className' => 'Productsbrands',
            'foreignKey' => 'productsbrands_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'Productsrange' => array(
            'className' => 'Productsranges',
            'foreignKey' => 'productsranges_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'Productscategory' => array(
            'className' => 'Productscategories',
            'foreignKey' => 'productscategories_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'Suppliers' => array(
            'className' => 'Suppliers',
            'foreignKey' => 'suppliers_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

    /**
     * hasAndBelongsToMany associations
     *
     * @var array
     */
    public $hasAndBelongsToMany = array(
        'Orders' => array(
            'className' => 'Orders',
            'joinTable' => 'Ordersproducts',
            'foreignKey' => 'products_id',
            'associationForeignKey' => 'orders_id',
            'unique' => 'keepExisting',
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'finderQuery' => '',
            'deleteQuery' => '',
            'insertQuery' => ''
        )
    );

    public $hasMany = array(
        'Productsinstance' => array(
            'className' => 'Productsinstances',
            'foreignKey' => 'products_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'Gift' => array(
            'className' => 'Gifts',
            'foreignKey' => 'products_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

    public function belowMax($check) {
        $min = $this->data['Product']['min_level'];
        $max = $this->data['Product']['max_level'];
        return $min <= $max;
    }

    function afterFind($results, $primary=false) 
    { 
        foreach ($results as $key => $val) 
        { 
            if (isset($val['Order']['created'])) 
            { 
                $results[$key]['Order']['created'] = $this->dateFormatAfterFind($val['Order']['created']); 
            } 
        } 
        return $results; 
    } 

    function dateFormatAfterFind($dateString) 
    { 
        return date('d/m/Y', strtotime($dateString)); 
    } 

}